define(['app','pagination','datetime','laydate','autocomplete'], function( app ) {
	'use strict';
	app.controller('Xufei', function($scope, dataFactoryCRM, $q ) {
	
		$scope.richtxtAdd = '';
		$scope.searchStr = '';
		$scope.slist_tag ;
		$scope.search_nickname="";
		$scope.search_sale_person="";
		$scope.search_create_time="";
		$scope.search_customer_type ="";
		$scope.search_tag_name = "";
		$scope.tab = 'xufei';
		$scope.key = "1";

		$('.ui.selection.dropdown').dropdown();
		$scope.XufeiPage = {pageAll: 1,pageNow: 1,pageSize: 10,k: 3};
		$scope.LogPage = {pageAll: 1,pageNow: 1,pageSize: 8,k: 3};
		$scope.XuFeiList = function(key) {
			$scope.$emit("BUSY");
			if($scope.XufeiPage.pageNow == "" || $scope.XufeiPage.pageNow ==undefined){
				$scope.XufeiPage.pageNow = $scope.getdata.p;
				$scope.XufeiPage.pageSize = $scope.getdata.psize;
			}
			if($scope.search_nickname =="" && $scope.search_tag_name=="" && $scope.search_create_time =="" && key!=undefined){
				$scope.key = key;
			}
			if($("#create_time").val()!=""){$scope.search_create_time = $("#create_time").val();}
			var _url = "Xufei/index?id="+$scope.key +"&login_name="+$scope.search_nickname+"&tag_name="+$scope.search_tag_name+"&create_time="+$scope.search_create_time+'&p=' + $scope.XufeiPage.pageNow + '&psize=' + $scope.XufeiPage.pageSize;
			$scope.busy = dataFactoryCRM.getDataByAPI(_url).then(
		        function(data){
		        	if(data.rcode=="1" && data.list[0].create_time != false){

		        		$scope.getdata = data.getdata;

		        		$("#y_xufei_list").show();
		        		$("#w_xufei_list").hide();
		        		$("#w_xufei_lister").show();

		        		$scope.Customer = data.list;
			          	$scope.XufeiPage.pageSize = data.psize;
						$scope.XufeiPage.pageAll = data.page;
						$scope.XufeiPage.pageNow = data.p;
		        	}else{
		        		$("#y_xufei_list").hide();
	        			$("#w_xufei_list").show();
	        			$("#w_xufei_lister").hide();
		        	}
		        	$scope.$emit("NOTBUSY");
		        }
		    )
	    }
		$scope.XuFeiList(1);
	
		$scope.tab_select = function (customer_xufei_id,customer_id,user_id){
			//修改标签加载
			$("#tag_select").val("");
			$scope.busy = dataFactoryCRM.getDataByAPI('Xufei/Tab_Select?customer_xufei_id=' + customer_xufei_id + "&customer_id=" + customer_id+"&user_id="+user_id).then(
		        function(data){
		        	 $scope.slist_tag = data.slist;
		        	 $scope.tag_child = data.child;
		         	 $scope.list_tag = data.list;
		         	 $scope.new_list = data.new_list;
		         	 $scope.tag_customer = data.tag_customer;
		    			//清除原数据
		         	 $('#multiple_dropdown a').remove();
		         	 for(var z = 0;z<$scope.slist_tag.length;z++){
		    			//清除原数据
		         	 	$('#item'+$scope.slist_tag[z].tag_id).remove();
		         	 }

		         	 if($scope.list_tag !="" &&  $scope.tag_child !="" ){
		         	 	$("#tag_select").val($scope.tag_child);
		         	 	$("#default_text").text("");
		         	 	if($scope.NewTagList == "" || $scope.NewTagList == undefined){
		         	 		for(var i=0;i<$scope.slist_tag.length;i++){
			         	 		var v = $scope.slist_tag[i];
			         	 		$("#menu_dropdown").append("<div class='item' id='item"+v.tag_id+"' data-value="+v.tag_id+" onclick='CheckTags("+v.tag_id+")'>"+v.tag_name+"</div>");
		         	 		}
				         	
			         	 	for(var s=0;s<$scope.list_tag.length;s++){
			         	 	 	var list = $scope.list_tag[s];
			         	 	 	$("#multiple_dropdown").append("<a class='ui label transition visible' data-value='"+list.tag_id+"' style='display: inline-block !important;'>"+list.tag_name+"<i class='delete icon' onclick='AddTags("+list.tag_id+","+$scope.tag_customer.customer_id+")'></i></a>");
		      			 	}

		      			 	for (var x = 0; x < $scope.slist_tag.length ; x++) {
		      			 		for (var z=0; z < $scope.list_tag.length; z++) {
		      			 			if($scope.slist_tag[x].tag_id == $scope.list_tag[z].tag_id){
		      			 				$("#item"+$scope.slist_tag[x].tag_id).hide();
		      			 			}
		      			 		};
		      			 	};
		         	 	}else{
		         	 		$('#menu_dropdown div').remove();
		         	 		 $scope.new_assignment = $("#tag_select").val();
		         	 		 
				    	    var words = $scope.new_assignment.split(',');//分割字符串
		         	 		for(var i=0;i<$scope.NewTagList.length;i++){
			         	 		var v = $scope.NewTagList[i];
			         	 		$("#menu_dropdown").append("<div class='item' id='item"+v.tag_id+"' data-value="+v.tag_id+" onclick='CheckTags("+v.tag_id+")'>"+v.tag_name+"</div>");
		         	 		}
		         	 		
		         	 		for(var i=0;i<$scope.NewTagList.length;i++){
			         	 		for(var s=0;s<words.length;s++){
			         	 			if($scope.NewTagList[i].tag_id == words[s]){
			         	 				$("#multiple_dropdown").append("<a class='ui label transition visible' data-value='"+$scope.NewTagList[i].tag_id+"' style='display: inline-block !important;'>"+$scope.NewTagList[i].tag_name+"<i class='delete icon' onclick='AddTags("+$scope.NewTagList[i].tag_id+","+$scope.tags_customer.customer_id+")'></i></a>");
			         	 				$("#item"+$scope.NewTagList[i].tag_id).hide();
			         	 			}
			         	 		}
			         	 	}
		         	 	}
		         	 }else{
		         	 	for(var i=0;i<$scope.slist_tag.length;i++){
	  						var v = $scope.slist_tag[i];
	  						$("#menu_dropdown").append("<div class='item'  data-value="+v.tag_id+" onclick='CheckTags("+v.tag_id+")'>"+v.tag_name+"</div>");
		      			}
		         	 }
		        	 $scope.XuFeiList();
		        }
		    );
		}
		$scope.Check_Tag = function(){
			//添加标签
			$scope.change_tag = $("#tag_select").val();
			dataFactoryCRM.getDataByAPI('Xufei/Check_Tag?customer_id='+ $scope.tag_customer.customer_id + "&array_tag="+$scope.change_tag + "&user_id="+$scope.tag_customer.user_id)
			    .then( function( data ) {
			    	if(data.rcode == "1"){
			    		$('#MyDiv').hide();
			    		$("#fade").hide();
			    		$scope.XuFeiList();
			    		return true;
			    	}else{
			    		$scope.productMsg = data.rcode;
			    		$scope.productRcode = 0;
			    		$scope.msgAttention();
			    	}
		       }
	       );	
		}
		$scope.Search_Tag = function(){
			if($('#another .message').text() ){
				//新建标签
				dataFactoryCRM.getDataByAPI('Xufei/New_Tag?customer_id='+ $scope.tag_customer.customer_id + "&new_tag_name="+$scope.new_tag_name + "&user_id="+$scope.tag_customer.user_id)
				    .then( function( data) {

				    	if(data.msg=="1"){
				    		for(var z = 0;z<$scope.slist_tag.length;z++){
				    			//清除原数据
				         	 	$('#item'+$scope.slist_tag[z].tag_id).remove();
				         	 }

				    		$scope.NewTag = data.rcode;//新建的标签
				    		$scope.tags_customer = data.tag_customer;
				    	 	$scope.NewTagList = data.lists;//全部信息
				   			$scope.NewCustomerTag = data.customer_tag;//客户默认的tag
				   			
				    	 	$scope.assignment = $("#tag_select").val();
				    	 
				    		 var sear = new RegExp(',');
				    	    if(sear.test($scope.assignment)){
				    	 		$("#tag_select").val($scope.assignment+','+$scope.NewTag.tag_id);
				    	 		$("#multiple_dropdown").append("<a class='ui label transition visible' data-value='"+$scope.NewTag.tag_id+"' style='display: inline-block !important;margin-left:5px;'>"+$scope.NewTag.tag_name+"<i class='delete icon' onclick='AddTags("+$scope.NewTag.tag_id+","+$scope.tag_customer.customer_id+")'></i></a>");

				    	    }else{
				    	    	if($("#tag_select").val() =="" || $("#tag_select").val() == undefined){
				    	    		$("#tag_select").val($scope.NewTag.tag_id);
				    	    	}else{
				    	    		$("#tag_select").val($scope.assignment+','+$scope.NewTag.tag_id);
				    	    	}
				    	    	
				    	    	$("#multiple_dropdown").append("<a class='ui label transition visible' data-value='"+$scope.NewTag.tag_id+"' style='display: inline-block !important;margin-left:5px;'>"+$scope.NewTag.tag_name+"<i class='delete icon' onclick='AddTags("+$scope.NewTag.tag_id+","+$scope.tag_customer.customer_id+")'></i></a>");
				    	    }

				    	    $scope.new_assignment = $("#tag_select").val();
				    	    if($scope.new_assignment !=""){
				    	    	$("#default_text").text("");
				    	    }else{
				    	    	$("#default_text").text("Select Tags");
				    	    }
				    	    var words = $scope.new_assignment.split(',');//分割字符串
		         	 		if($scope.new_assignment.indexOf(",") > 0){
					         	 //全部数据
		         	 			for(var i=0;i<$scope.NewTagList.length;i++){
				         	 		var v = $scope.NewTagList[i];
				         	 		$("#menu_dropdown").append("<div class='item' id='item"+v.tag_id+"' data-value="+v.tag_id+" onclick='CheckTags("+v.tag_id+")'>"+v.tag_name+"</div>");
			         	 		}
			         	 		//隐藏的数据
				    			for (var x = 0; x < $scope.NewTagList.length ; x++) {
			      			 		for (var z=0; z < $scope.new_assignment.length; z++) {
			      			 			if($scope.NewTagList[x].tag_id == $scope.new_assignment[z]){
			      			 				$("#item"+$scope.NewTagList[x].tag_id).hide();
			      			 			}
			      			 		};
			      			 	};
				    		}else{
				    			$('#menu_dropdown div').remove();
				    			  //全部数据
		         	 			for(var i=0;i<$scope.NewTagList.length;i++){
				         	 		var v = $scope.NewTagList[i];
				         	 		$("#menu_dropdown").append("<div class='item' id='item"+v.tag_id+"' data-value="+v.tag_id+" onclick='CheckTags("+v.tag_id+")'>"+v.tag_name+"</div>");
			         	 		}
			         	 		for(var i=0;i<$scope.NewTagList.length;i++){
				         	 		for(var s=0;s<words.length;s++){
				         	 			if($scope.NewTagList[i].tag_id == words[s]){
				         	 				$("#item"+$scope.NewTagList[i].tag_id).hide();
				         	 			}
				         	 		}
				         	 	}
				    		}
				    	}else{
				    		alert(data.rcode);
				    	}
				    	$scope.XuFeiList();
			       }
		       );
			}
		}

		$scope.Change_Star = function(key){
			//更改星标
			dataFactoryCRM.getDataByAPI('Xufei/Change_Star?customer_xufei_id='+key)
			    .then( function( data ) {
			    	if(data.rcode =="1"){
			    		if(data.old_flag == "1"){
			    			$("#star"+data.xufei_id).attr("src","images/starky.png");
			    		}else{
			    			$("#star"+data.xufei_id).attr("src","images/starsy.png");
			    		}
			    	}
			    	$scope.XuFeiList();
		       }
	       );	
		}

		$scope.CustomerTypeShow = function(customer_xufei_id,customer_id,customer_note){
			//资料维护加载			
			var _vproduct = this.list;
			$('.ui.CustomerTypeShow input[name="customr_mount"]').val(_vproduct.mount);
			$('.ui.CustomerTypeShow input[name="xcustomer_note"]').val(_vproduct.note);
			$('.ui.CustomerTypeShow input[name="customer_xufei_id"]').val(_vproduct.customer_xufei_id);
			$('.ui.CustomerTypeShow input[name="customer_id"]').val(_vproduct.customer_id);
			$("#record input").val("");
			$("#communication").val("");
			$("#recommended").val("");
			if(customer_xufei_id == "" || customer_xufei_id==undefined){
				var customer_xufei_id = $scope.getdata.customer_xufei_id;
				var customer_id = $scope.getdata.customer_id;
				var customer_note = $scope.getdata.customer_note;
			}
        	           
			var _url = "Xufei/CustomerTypeShow?customer_xufei_id="+customer_xufei_id+"&customer_id="+customer_id+"&customer_note="+customer_note+"&p=" + $scope.LogPage.pageNow + '&psize=' + $scope.LogPage.pageSize;
			dataFactoryCRM.getDataByAPI(_url)
			    .then( function(data) {
			     	$scope.getdata = data.getdata;
			    	$scope.customer_type = data.customer_type;
			    	$scope.LogPage.pageSize = data.psize;
					$scope.LogPage.pageAll = data.page;
					$scope.LogPage.pageNow = data.p;
			   		for(var x = 0;x<$scope.customer_type.length;x++){
			   			if($scope.customer_type[x].id == _vproduct.customer_type ){
			   				$("#type"+$scope.customer_type[x].id).addClass("active selected");
			   				$("#default_type").text($scope.customer_type[x].name);
			   				$("#save_type").val($scope.customer_type[x].id);
			   			}
			   		}
			   		if(data.customerLog !="" && data.customerLog != undefined){
			   			$scope.customerLog = data.new_log;
			   			$("#recordShow").show();
			   			$("#recordHide").hide();
			   		}else{
			   			$("#recordShow").hide();
			   			$("#recordHide").show();
			   		}
		       }
	       );

		  $('.ui.CustomerTypeShow').modal('show');
		}

		$scope.ChangeCustomerType = function(){
			//更改客户类型
			$scope.$emit("BUSY");
			$scope.customer_xufei_id = $('.ui.CustomerTypeShow input[name="customer_xufei_id"]').val();
			$scope.customer_id = $('.ui.CustomerTypeShow input[name="customer_id"]').val();
			$scope.new_type = $("#save_type").val();
			$scope.new_note = $("#xcustomer_note").val();
			$scope.new_mount = $("#customr_mount").val();
			dataFactoryCRM.getDataByAPI('Xufei/ChangeCustomerType?customer_xufei_id='+$scope.customer_xufei_id+"&new_type="+$scope.new_type+"&new_note="+$scope.new_note+"&customer_id="+$scope.customer_id+"&mount="+$scope.new_mount)
			    .then( function(data) {
			    	if(data.rcode == "1"){
			    		$(".ui.CustomerTypeShow").modal('hide');
			    		$("#all").addClass("selected").siblings().removeClass("selected"); 
			    		$(".ui.CustomerTypeShow").modal('hide');
			    		$scope.XuFeiList();
			    		return true;
			    	}else{
			    		$scope.productMsg = "修改失败!!!";
			    		$scope.productRcode = 0;
			    		$scope.msgAttention();
			    	}
			    	 $scope.$emit("NOTBUSY");
		       }
	       );	
		}
		$scope.AddCommunication = function(){
			//添加沟通记录
			$scope.$emit("BUSY");
			var communication = $("#communication").val();
			var recommended = $("#recommended").val();
			$scope.customer_id = $('.ui.CustomerTypeShow input[name="customer_id"]').val();
			if(communication !="" && recommended!=""){
				dataFactoryCRM.getDataByAPI('Xufei/AddCommunication?customer_id='+$scope.customer_id+'&communication='+communication+"&recommended="+recommended)
				    .then( function(data) {
				    	if(data.rcode == 1){
				    		$(".ui.CustomerTypeShow").modal('hide');
				    		$("#all").addClass("selected").siblings().removeClass("selected"); 
				    		$scope.XuFeiList();
				    		return true;
				    	}else{
				    		$scope.productMsg = data.rcode;
				    		$scope.productRcode = 0;
				    		$scope.msgAttention();
				    	}
				    $scope.$emit("NOTBUSY");		
			       }
		       );	
			}else{
				$scope.msgAttention();
				$scope.productRcode = 0;
				if(communication ==""){
					$scope.productMsg = "沟通记录不能为空！！！";
				}else{
					$scope.productMsg = "推荐股票不能为空！！！";
				}
			}
			
		}
		 $scope.hover_data = function(customer_xufei_id,key){
	        //推荐框加载
			dataFactoryCRM.getDataByAPI('Xufei/Holding?customer_xufei_id='+customer_xufei_id)
	    		.then(function(data) {
		    	if(data.rcode=="1"){
		    		$scope.stock = data.list;
		    	}else{
		    		$scope.stock = "";
		    	}
	        })		        
	        $('#See'+key).popup({
	           hoverable: true,delay: {show: 100,hide: 500},
	           position: 'left center',
	        });
	    }
	    $scope.StockShow = function(key){
	    	$('#frm-addStock input').val('');
			$('#frm-addStock textarea').val('');
			dataFactoryCRM.getDataByAPI('Xufei/StockShow')
	    		.then(function(data) {
	    		$scope.customer_id = key;
		    	$scope.stock_list = data.stock_list;
	        })		
			$('.ui.addStock').modal({
				onApprove: function() {
					if ($('#frm-addStock').form('is valid')) {
						$scope.addStock( $('#frm-addStock').serialize() + '&stock_code=' + $('#addStock').val() + '&stock_name=' + $('#addStock_name').val()+ '&customer_id=' + $scope.customer_id);
						return true;
					} else {
						return false;
					}
				}
			}).modal('show');
	    }
	    $scope.addStock = function(_d){
	    	//添加推荐
	    	$scope.$emit("BUSY");
	    	dataFactoryCRM.postDataByAPI('View360/buyStock',_d)
	    		.then(function(data) {
	  			if(data.rcode == "1"){
		    		$scope.XuFeiList();
		    		return true;
	  			}else{
	  				$scope.productMsg = "添加失败!!!";
		    		$scope.productRcode = 0;
		    		$scope.msgAttention();
	  			}
	  			 $scope.$emit("NOTBUSY");
	        })		
	    }
	    /*购买股票自动补全*/
		$scope.selectedStock = function(selected) {
			if (selected) {
				$('#addStock').val(selected.originalObject.stock_code);
				$('#addStock_name').val(selected.originalObject.stock_name);
			}
		};
				/*系统调用*/
		$scope.msgAttention = function() {
			$('.ui.systemerror').modal({
				closable: false,
				onDeny: function() {
					window.alert('Wait not yet!');
					return false;
				},
				onApprove: function() {
					$('.ui.dimmer.modals.page.transition.visible.active.dimmers').removeClass('dimmers');
				}
			}).modal('show');

			setTimeout(function() {
				var system = $('.ui.dimmer.modals.page.transition.visible.active');
				if (system) {
					$('.ui.dimmer.modals.page.transition.visible.active').addClass('dimmers');
				}
			}, 1000);
		};
		$scope.Search_Tag2 = function(){
			//事件监听
			$scope.new_tag_name = $(".sizer").text();	
		};
		$scope.close = function(){
			//页面关闭
			$('.ui.TagShow').modal('hide');
			$('.ui.modal.Eidt').modal('hide');
			$('.ui.modal.Eidt_sale_person').modal('hide');
			$(".ui.CustomerTypeShow").modal('hide');
  			$("#fade").hide();
  			$("#MyDiv").hide();
		}
	
	})
	
});